<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head th:include="common/common :: bootcss">
    <title>充值赠送配置</title>
    <script>
        function queryChargeGive () {
            $.post("/admin/chargeGive/list", function(data){
                if (!data.success) {
                    console.log(data.errorMsg);
                } else {
                    var html = "";
                    data.data.forEach(function (e) {
                        html += "<tr>";
                        html += "<td>" + e.id + "</td>";
                        html += "<td>" + e.chargeAmt/100 + "元</td>";
                        html += "<td>" + e.giveAmt/100 + "元</td>";
                        html += "<td><button type='button' class='btn btn-xs' onclick='showChargeGive(" + e.id + "," + e.chargeAmt/100 + "," + e.giveAmt/100 + ")' data-toggle='modal' data-target='#chargeGiveModal'>修改</button>" +
                            " <button type='button' class='btn btn-xs' onclick='delChargeGive(" + e.id + ")'>删除</button></td>";
                        html += "</tr>";
                    });
                    $(".table > tbody").html(html);
                }
            })
        }

        function showChargeGive(id, chargeAmt, giveAmt) {
            $('form').data('bootstrapValidator').resetForm(true);
            $(".modal-title").html("修改");
            $("#dt-id").val(id);
            $("#dt-chargeAmt").val(chargeAmt);
            $("#dt-giveAmt").val(giveAmt);
        }
        function delChargeGive(id) {
            $.post("/admin/chargeGive/del", {
                id : id
            }, function (data) {
                if (!data.success) {
                    console.log(data.errorMsg);
                } else {
                    queryChargeGive();
                }
            });
        }

        $(document).ready(function () {

            queryChargeGive();
            $("#btRefresh").click(function() {
                queryChargeGive();
            });
            $("#btAdd").click(function(){
                $('form').data('bootstrapValidator').resetForm(true);
                $(".modal-title").html("添加");
                $("#dt-id").val("");
                $("#dt-chargeAmt").val("");
                $("#dt-giveAmt").val("");
            });
            $("#btSaveAndUpdate").click(function () {
                if ($("form").data('bootstrapValidator').isValid()) {
                    $.post("/admin/chargeGive/saveOrUpdate", {
                        id : $("#dt-id").val(),
                        chargeAmt : $("#dt-chargeAmt").val() * 100,
                        giveAmt : $("#dt-giveAmt").val() * 100
                    }, function (data) {
                        if (!data.success) {
                            console.log(data.errorMsg);
                        } else {
                            queryChargeGive();
                            $('#chargeGiveModal').modal('hide');
                        }
                    });
                } else {
                    $('form').bootstrapValidator('validate');
                }
            });

            $("form").bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                excluded: [':disabled'],
                fields: {
                    chargeAmt : {
                        validators: {
                            notEmpty: {
                                message: '充值金额不能为空'
                            },
                            regexp: {
                                regexp: /^\d+$/,
                                message: '金额必须是数字'
                            },
                        }
                    },
                    giveAmt : {
                        validators: {
                            notEmpty: {
                                message: '赠送金额不能为空'
                            },
                            regexp: {
                                regexp: /^\d+$/,
                                message: '金额必须是数字'
                            },
                        }
                    }
                }
            });
        })

    </script>
</head>
<body>
<ol class="breadcrumb">
    <li><a th:href="@{/admin/chargeGive/}">充值赠送配置</a></li>
</ol>
<button type="button" class="btn btn-default" id="btRefresh" style="margin-bottom: 10px">刷新</button>
<button type="button" class="btn btn-default" id="btAdd" style="margin-bottom: 10px" data-toggle='modal' data-target='#chargeGiveModal'>添加</button>
<table class="table">
    <thead>
    <tr>
        <th>ID</th>
        <th>充值金额</th>
        <th>赠送金额</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>

    </tbody>
</table>

<div class="modal fade" tabindex="-1" role="dialog" id="chargeGiveModal">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">充值赠送</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" id="dt-id">
                    <div class="form-group">
                        <label for="dt-chargeAmt">充值金额</label>
                        <input type="text" class="form-control" name="chargeAmt" id="dt-chargeAmt" >
                    </div>
                    <div class="form-group">
                        <label for="dt-giveAmt">赠送金额</label>
                        <input type="text" class="form-control" name="giveAmt" id="dt-giveAmt" >
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="btSaveAndUpdate">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>